//ACV_init.sqf
#include "ACV_constantes.h"
private ["_groupLogic","_void","_null"];
call compile preprocessFile "tmf\core\modules\r3f_acv\ACV_configuration.sqf";
diag_log format [
	"-------------------- %1 - A.C.V v%2 --------------------", 
	getText (missionConfigFile >> "onLoadMission"),
	getText (missionConfigFile >> "ACV_Version")
];
if (isNil "ACV_User") then {ACV_User = false};
if (isNil "ACV_Granted") then {ACV_Granted = false};
createCenter sideLogic;
if (isnil "bis_fnc_init") then {
	activateAddons ["ca_modules_functions"];
	_groupLogic = createGroup sideLogic;
	sleep 0.03;
	_void = _groupLogic createUnit ["FunctionsManager",[0,0,0],[],0,"NONE"];
	waituntil {!(isnil "bis_fnc_init")};
};
if (isnil "IS_INIT") then {
	IS_INIT = false;
	IS_Server = false;
	IS_Client = false;
	IS_JIP = false;
	if (playersNumber east + playersNumber west + playersNumber resistance + playersNumber civilian > 0) then {IS_MP = true} else {IS_MP = false};
	if (isServer) then {
		IS_Server = true;
		if (!(isNull player)) then {IS_Client = true};
		IS_INIT = true;
	} else {
		IS_Client = true;
		if (isNull player) then {
		IS_JIP = true;
		[] spawn {waitUntil {!(isNull player)}; IS_INIT = true};
		} else {
			 IS_INIT = true;
		};
	};
	waitUntil {IS_INIT};
};
if (IS_Server) then {
	_null = [] execVM "tmf\core\modules\r3f_acv\Scripts\ACV_Init_Server.sqf";
};
_null = [] execVM "tmf\core\modules\r3f_acv\Scripts\ACV_Init_Common.sqf";
"ACV_NET_BroadcastMSG" addPublicVariableEventHandler {
	[(_this select 1)] call ACV_FNCT_ShowMessage;
};
if (IS_Client) then {
	// player addEventHandler ["Killed", {
		// [] spawn {
			// waitUntil {alive player;};
			// sleep 5;
			// call ACV_InitPlayer;
		// };
	// }];
	[] spawn {
		private ["_allowed"];
		// diag_log Format["ACV:  (%1 || %2 || %3 || %4 || %5) - %6",(serverCommandAvailable "#shutdown"), (IS_Server AND IS_Client), (!isMultiplayer), (!ACV_AdminOnly),ACV_Granted, ACV_User];
		_allowed = false;
		while {true} do {
			// Several checks are done:
			// 1) The user is logged in as admin
			// 2) The user hosts the game
			// 3) We are not in multiplayer
			// 4) Everyone has access to the ACV or not.
			if (!_allowed && !isnil "mp_rights_fnc_mustHaveRoles") then {
				if (ACV_AdminOnly) then {
					_allowed = ([player] call mp_rights_fnc_isAdmin);
				} else {
					_allowed = ([player,["admin","acv"]] call mp_rights_fnc_mustHaveRoles);
				};
			};
			if (!_allowed && !isnil "mso_core_fnc_isadmin") then {
				_allowed = ([] call mso_core_fnc_isadmin);
			};
			if (!_allowed) then {
				_allowed = (serverCommandAvailable "#kick" || !isMultiplayer);
			};
			if (!_allowed && !ACV_AdminOnly && ACV_Granted) then {
				_allowed = true;
			};
			If (_allowed) then {
				If (!ACV_User) then { diag_log "ACV User Enabled"; };
				ACV_User = True;
			} else {
				If (ACV_User) then { diag_log "ACV User Disabled"; };
				ACV_User = False;
			};
			sleep 15;
			_allowed = false;
		};
	};
	_null = [] execVM "tmf\core\modules\r3f_acv\Scripts\ACV_Init_Client.sqf";
};